package com.framework.foundation.common.vo;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.framework.foundation.common.ConstantResult;

/**
 * 返回结果 用于mvc 返回统一的结构的结果对象
 * 
 */
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Result<T> implements IResult<T> {

	/**
	 * <code>serialVersionUID</code> of comment
	 */
	private static final long serialVersionUID = 1L;
	// 执行成功
	public static final int SUCCESS = 0;
	// 执行失败
	public static final int FAIL = 1;

	protected T data;
	protected int code;

	public static <T> Result createSuccess() {
		return new Result(SUCCESS);
	}

	public static <T> Result createSuccess(T data) {
		return new Result(SUCCESS, data);
	}

	public static <T> Result createFail() {
		return new Result(FAIL);
	}

	public static <T> Result createFail(T data) {
		return new Result(FAIL, data);
	}

	public static <T> Result create(int code, T data) {
		return new Result(code, data);
	}

	public static <T> Result create(int code) {
		return new Result(code);
	}

	public static <T> Result create(Boolean code, T data) {
		return new Result(((code != null) && code) ? SUCCESS : FAIL, data);
	}

	public static <T> Result create(Boolean code) {
		return new Result(((code != null) && code) ? SUCCESS : FAIL);
	}

	public static <T> Result create(ConstantResult cr) {
		return new Result(cr.getCode(), cr.getData());
	}

	private Result(int code) {
		super();
		this.code = code;
	}

	private Result(T data) {
		super();
		this.data = data;
		this.code = SUCCESS;
	}

	private Result(int code, T data) {
		super();
		this.code = code;
		this.data = data;
	}

	@Override
	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	@Override
	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	@Override
	public boolean success() {
		return this.code == SUCCESS;
	}

}